home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / mac / Code / Chapter08 / RotateMovePanel.java < prev    next >
Text File  |  2000-07-01  |  2KB  |  129 lines

  1.  
  2.  
  3.  
  4. package applets;
  5.  
  6. import shout3d.*;
  7. import shout3d.core.*;
  8. import shout3d.math.*;
  9.  
  10.  
  11. public class RotateMovePanel extends Shout3DPanel implements DeviceObserver{
  12.     
  13.     Transform mover;
  14.  
  15.     int pixelStartX;
  16.     int pixelStartY;
  17.     int pixelEndX;
  18.     int pixelEndY;
  19.  
  20.     float[] worldPos = new float[3];
  21.  
  22.     float [] eulers = new float [3];
  23.     float [] axisAngle = new float [4];
  24.     Quaternion q = new Quaternion();
  25.  
  26.  
  27.     public RotateMovePanel (Shout3DApplet applet){
  28.         super(applet);
  29.     }
  30.     
  31.         public void customInitialize() {
  32.  
  33.         addDeviceObserver(this,"MouseInput", null);
  34.  
  35.         mover = (Transform) getNodeByName("vehicle");
  36.  
  37.         worldPos = mover.translation.getValue();
  38.  
  39.         axisAngle = mover.rotation.getValue();
  40.         q.setAxisAngle(axisAngle);
  41.         q.getEulers(eulers);
  42.             
  43.     }
  44.  
  45.  
  46.  
  47.     protected void finalize()  {
  48.  
  49.         removeDeviceObserver(this,"MouseInput");
  50.  
  51.     }
  52.  
  53.  
  54.     public boolean onDeviceInput(DeviceInput di, Object userData) {
  55.  
  56.         MouseInput mi = (MouseInput) di;
  57.  
  58.         switch (mi.which){
  59.             case MouseInput.DOWN:
  60.                 pixelStartX = mi.x;
  61.                 pixelStartY = mi.y;
  62.                 return true;                                        
  63.  
  64.  
  65.             case MouseInput.DRAG:
  66.  
  67.                 //PIXEL OPERATIONS
  68.                 pixelEndX = mi.x;
  69.                 pixelEndY = mi.y;
  70.                 
  71.                 int dragDistanceX = pixelEndX - pixelStartX;
  72.                 int dragDistanceY = pixelEndY - pixelStartY;
  73.  
  74.                 pixelStartX = pixelEndX;
  75.                 pixelStartY = pixelEndY;
  76.  
  77.  
  78.                 //ROTATING THE OBJECT
  79.  
  80.                 //convert x drag to rotation
  81.                 // at 30 pixels/radian
  82.                 float rotationDelta = -(dragDistanceX/30f);
  83.  
  84.                 //compute new heading
  85.                 eulers[0] = eulers[0] + rotationDelta;
  86.  
  87.                 //Convert from Eulers to AxisAngle
  88.                 // using the Quaternion
  89.                 q.setEulers(eulers);
  90.                 q.getAxisAngle(axisAngle);
  91.  
  92.                 //set the Transform to
  93.                 //updated axis angle values
  94.                 mover.rotation.setValue(axisAngle);
  95.  
  96.  
  97.  
  98.                 //TRANSLATING THE OBJECT
  99.  
  100.                 //covert y drag to distance
  101.                 //at 2 meters per pixel
  102.                 float translationDelta = dragDistanceY * 2.0f;
  103.  
  104.                 //make a vector pointing
  105.                 //in z direction
  106.                 float[] vector = {0,0, translationDelta};
  107.  
  108.                 //rotate the vector using
  109.                 //value in quaternion
  110.                 q.xform(vector);
  111.  
  112.                 //add rotated vector
  113.                 //to current position
  114.                 worldPos[0] = worldPos[0] + vector[0];
  115.                 worldPos[2] = worldPos[2] + vector[2];
  116.  
  117.  
  118.                 //set Transform to new position
  119.                 mover.translation.setValue(worldPos);
  120.  
  121.     
  122.                 return true;
  123.  
  124.         }//end of switch
  125.  
  126.         return false;        
  127.     }
  128.  
  129. } //end of class